import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:tencent_trtc_cloud/trtc_cloud_def.dart';
import 'package:tencent_trtc_cloud/trtc_cloud_video_view.dart';
import 'logic.dart';

///@author:create by BZY
///Date: 2021/7/22 17:04
///Description: 实时音视频组件
class VideoWidget extends StatelessWidget {
  final VideoLogic controller = Get.put<VideoLogic>(VideoLogic());

  ///左上方的操作组件
  final Widget? actions;

  ///底部的操作组件
  final Widget? actionsBottom;

  ///抛出需要的事件
  final Function(dynamic)? onEvent;

  VideoWidget({this.actions, this.actionsBottom, this.onEvent});

  @override
  Widget build(BuildContext context) {
    controller.onEvent = onEvent;
    return Stack(
      children: [
        GetBuilder<VideoLogic>(
          init: controller,
          id: 'localVideo',
          builder: (controller) {
            return controller.state.hasEnterRoom
                ? Container(
              child: TRTCCloudVideoView(
                key: ValueKey('${controller.state.userId}'),
                viewType: TRTCCloudDef.TRTC_VideoView_SurfaceView,
                onViewCreated: (viewId) {
                  controller.startLocalPreview(viewId);
                },
              ),
            )
                : Container(
              color: Colors.black54,
            );
          },
        ),
        Align(
          alignment: Alignment.topRight,
          child: getRemoteView(),
        ),
        Align(
          alignment: Alignment.topLeft,
          child: actions,
        ),
        Align(
          alignment: Alignment.bottomCenter,
          child: bottomActions(),
        )
      ],
    );
  }

  ///远端视频流
  Widget getRemoteView() {
    return GetBuilder<VideoLogic>(
      init: controller,
      id: 'remoteVideo',
      builder: (controller) {
        return controller.state.isUserEnter
            ? Container(
          margin: EdgeInsets.only(right: 12, top: 36),
          width: 100,
          height: 150,
          child: TRTCCloudVideoView(
            key: ValueKey('${controller.state.remoteUserId}'),
            viewType: TRTCCloudDef.TRTC_VideoView_SurfaceView,
            onViewCreated: (viewId) {
              controller.startRemoteView(viewId);
            },
          ),
        )
            : Container();
      },
    );
  }

  ///底部操作组件
  Widget bottomActions() {
    return Container(
      height: 150,
      child: Column(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [statusView(), countDown(), actionsBottom ?? Container()],
      ),
    );
  }

  ///视频状态组件
  Widget statusView() {
    return Container(
      child: GetBuilder<VideoLogic>(
        init: controller,
        id: 'statusView',
        builder: (controller) {
          return Text(
            controller.state.status,
            style: TextStyle(color: Colors.white, fontSize: 14),
          );
        },
      ),
    );
  }

  ///计时组件
  Widget countDown() {
    return Container(
      margin: EdgeInsets.only(top: 12, bottom: 40),
      child: GetBuilder<VideoLogic>(
        init: controller,
        id: 'countDown',
        builder: (controller) {
          return Text(
            controller.state.showTime,
            style: TextStyle(color: Colors.white, fontSize: 14),
          );
        },
      ),
    );
  }
}
